from rest_framework import serializers

from goods.models import SKU


class CartsSerializer(serializers.Serializer):

    sku_id = serializers.IntegerField(min_value=True)
    count = serializers.IntegerField(min_value=True)
    selected = serializers.BooleanField(default=True)

    def validate(self, attrs):

        # 判断sku——id商品是否存在
        try:
            sku = SKU.objects.get(id=attrs['sku_id'])
        except:
            raise serializers.ValidationError('商品不存在')

        # 判断库存
        if attrs['count'] > sku.stock:
            raise serializers.ValidationError('商品库存不足')

        return attrs


class CartsListSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField(min_value=True,read_only=True)
    selected = serializers.BooleanField(default=True,read_only=True)

    class Meta:
        model=SKU
        fields="__all__"


class CartsDeleteSerializer(serializers.Serializer):
    sku_id = serializers.IntegerField(min_value=True)

